<template>
  <!-- 新增角色 -->
  <el-dialog
    title="编辑弹层"
    :visible="dialogVisible"
    width="500px"
    @close="closeDialog"
  >
    <template #default>
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
    </template>
    <!-- 底部 -->
    <template>
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="cancel">取消</el-button>
        <el-button size="small" type="primary" @click="confims">确定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addlistAPI, detailsAPI, ModifyTheAPI } from '@/api/management'
export default {
  name: 'AddRole',
  props: {
    dialogVisible: {
      type: Boolean
    }

  },
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    closeDialog() {
      this.$emit('close-dialog')
      this.$refs.roleForm.resetFields()
      // 清空数据
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 判断新增修改
    confims() {
      this.$refs.roleForm.validate(async(valid) => {
        if (valid) {
          if (this.roleForm.id) {
            await ModifyTheAPI(this.roleForm)
          } else {
            await addlistAPI(this.roleForm)
          }
          this.$emit('updata-List')
          this.$message.success(this.roleForm.id ? '修改成功' : '添加成功')
          this.resetForm()
        }
      })
    },

    // 取消
    cancel() {
      this.resetForm()
    },
    // 清空逻辑
    resetForm() {
      // 关闭弹框
      this.closeDialog()
      // 重置表单
      this.$refs.roleForm.resetFields()
      // 清空数据
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 接收editRole传过来的id
    async getRoleDetall(id) {
      const res = await detailsAPI(id)
      this.roleForm = res
    }

  }
}
</script>

<style>

</style>
